<?php
require_once '../RawsClient/Raws/Rams.php';
require_once '../RawsClient/Raws/Rass.php';

define('RAMS_BASE', 'http://rams.mon01.rambla.be/');
define('RASS_BASE', 'http://rass.cdn01.rambla.be/');
define('USER', 'xxx');
define('PWD', 'xxx');
define('FILEPATH', '/path/to/file.mp4');


try {
  $rams = new Rams(USER, PWD, RAMS_BASE);

  # get all http traffic for FILEPATH for the current month
  $query = new Rams_TrafficQuery();
  $query->setType("http");
  $query->setMonth("curr");
  $query->setYear("curr");
  $feed = $rams->getTrafficFeed(FILEPATH, $query);
  # RAMS should return a feed containing a single entry
  foreach ($feed as $entry) {
    echo "Volume: " . $entry->content->params->volume->text . "\n";
    echo "Hits: " . $entry->content->params->hits->text . "\n";
    # if traffic volume exceeds 0.5 Gb -> delete file from CDN
    if ($entry->content->params->volume->text > 0.5) {
      $rass = new Rass(USER, PWD, RASS_BASE);
      $rass->deleteItem(FILEPATH);
      echo "File deleted\n";
    }
  }    

}
catch(Zend_Gdata_App_Exception $e) {
    # Report the exception to the user
    echo "\nCaught exception: " . get_class($e) . "\n";
    echo "Message: " . $e->getMessage() . "\n";
    # get the HTTP status code
    echo "HTTP Status Code: " . $e->getResponse()->getStatus() . "\n";
    echo "Response Body with exception details: " . $e->getResponse()->getBody() . "\n";
    # get the raws:error elements
    $rawsResponse = Raws::parseRawsResponse($e->getResponse());
    echo "Raws Code: " . $rawsResponse->getCode() . "\n";
    echo "Raws Message: " . $rawsResponse->getMsg() . "\n";
    $reasons = $rawsResponse->getReasons();
    foreach ($reasons as $reason) {
      echo "Raws Reason: " . $reason . "\n";
    }
    $details = $rawsResponse->getDetails();
    foreach ($details as $key => $value) {
      echo "Raws Detail: " . $key . " -> " . $value . "\n";
    }
}
catch (Zend_Exception $e) {
    echo "Caught exception: " . get_class($e) . "\n";
    echo "Message: " . $e->getMessage() . "\n";
}
